﻿#include <iostream>
#include <string>
#include <utility>


/**
 * 设计模式之桥接模式(Bridge Pattern)
 * 用于把抽象化与实现化解耦，使得二者可以独立变化。
 * 这种类型的设计模式属于结构型模式，它通过提供抽象化和实现化之间的桥接结构，来实现二者的解耦。
*/

/// 葡萄
class Grape{
public:
    Grape() : m_area("china"), m_color("red"){}
    virtual ~Grape() = default;

    void setArea(std::string area) {
        m_area = std::move(area);
    }

    void setColor(std::string color) {
        m_color = std::move(color);
    }

    virtual void show() {
        std::cout << m_color << " Grape from " << m_area << std::endl;
    }

private:
    std::string m_area;   // 产地
    std::string m_color;  // 颜色
};


/// 紫葡萄
class PurpleGrape:public Grape{
public:
    explicit PurpleGrape(std::string area):Grape(){
        setColor("Purple");
        setArea(std::move(area));
    }
};

/// 绿葡萄
class GreenGrape: public Grape{
public:
    GreenGrape(std::string area):Grape(){
        setColor("Green");
        setArea(area);
    }
};

/// 酒
class Alcohol{
public:
    Alcohol(Grape* g):m_grape(g){}
    virtual ~Alcohol(){}
    virtual void show() = 0;
protected:
    Grape* m_grape;
};

/// 白酒
class Wine : public Alcohol{
public:
    Wine(Grape* p):Alcohol(p){}
    void show(){
        std::cout<<"wine is made of ";
        m_grape->show();
        std::cout<<std::endl;
    }
};

/// 香槟
class Champagne : public Alcohol{
public:
    Champagne(Grape* p):Alcohol(p){}
    void show(){
        std::cout<<"Champagne is made of ";
        m_grape->show();
        std::cout<<std::endl;
    }
};


int main()
{
    Grape* g1 = new PurpleGrape("Bordeau");  // 波尔多红酒
    Grape* g2 = new GreenGrape("Loire");     // Loire葡萄酒

    Alcohol* a1 = new Wine(g1); //将两个类结合在一起
    Alcohol* a2 = new Champagne(g2); //将两个类结合在一起

    a1->show();
    a2->show();
    return 0;
}
